package com.corticon.dialogs.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.corticon.dialogs.dao.FunctionDAO;
import com.corticon.dialogs.dao.LocaleDAO;
import com.corticon.dialogs.dao.ProductTypeDAO;
import com.corticon.dialogs.dao.RegionDAO;
import com.corticon.dialogs.dao.ThemeDAO;
import com.corticon.dialogs.model.Function;
import com.corticon.dialogs.model.Locale;
import com.corticon.dialogs.model.ProductType;
import com.corticon.dialogs.model.Region;
import com.corticon.dialogs.model.Theme;
import com.corticon.dialogs.util.Pager;

@Service("lookupService")
public class LookupServiceImpl implements LookupService {

    @Resource(name = "themeDAO")
    private ThemeDAO themeDAO;
    @Resource(name = "regionDAO")
    private RegionDAO regionDAO;
    @Resource(name = "functionDAO")
    private FunctionDAO functionDAO;
    @Resource(name = "localeDAO")
    private LocaleDAO localeDAO;
    @Resource(name = "productTypeDAO")
    private ProductTypeDAO productTypeDAO;

    @Override
    @Transactional
    public boolean createTheme(Theme theme) {
        themeDAO.save(theme);
        return false;
    }

    /**
     * Get all of the Theme {@link Pager} info. Currently, only get the number of all the
     * Theme.
     *
     * @return pager
     */
    @Override
    @Transactional
    public Pager getThemePagerAll() {
        Pager pager = new Pager();
        long countAll = themeDAO.getCountAll();
        pager.setNumOfRecords(countAll);
        return pager;
    }

    /**
     *
     * @param pager
     * @return
     */
    @Override
    @Transactional
    public List<Theme> listTheme(Pager pager) {
        List<Theme> themes = new ArrayList<Theme>();
        themes = themeDAO.getPageOfDataAll(pager);
        if (themes.size() > 1) {
            return themes;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean createRegion(Region region) {
        regionDAO.save(region);
        return false;
    }

    /**
     * Get all of the User {@link Pager} info. Currently, only get the number of all the
     * User.
     *
     * @return pager
     */
    @Override
    @Transactional
    public Pager getRegionPagerAll() {
        Pager pager = new Pager();
        long countAll = regionDAO.getCountAll();
        pager.setNumOfRecords(countAll);
        return pager;
    }

    /**
     *
     * @param pager
     * @return
     */
    @Override
    @Transactional
    public List<Region> listRegion(Pager pager) {
        List<Region> regions = new ArrayList<Region>();
        regions = regionDAO.getPageOfDataAll(pager);
        if (regions.size() > 1) {
            return regions;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean createFunction(Function function) {
        functionDAO.save(function);
        return false;
    }

    /**
     * Get all of the User {@link Pager} info. Currently, only get the number of all the
     * User.
     *
     * @return pager
     */
    @Override
    @Transactional
    public Pager getFunctionPagerAll() {
        Pager pager = new Pager();
        long countAll = functionDAO.getCountAll();
        pager.setNumOfRecords(countAll);
        return pager;
    }

    /**
     *
     * @param pager
     * @return
     */
    @Override
    @Transactional
    public List<Function> listFunction(Pager pager) {
        List<Function> functions = new ArrayList<Function>();
        functions = functionDAO.getPageOfDataAll(pager);
        if (functions.size() > 1) {
            return functions;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean createLocale(Locale locale) {
        localeDAO.save(locale);
        return false;
    }

    /**
     * Get all of the User {@link Pager} info. Currently, only get the number of all the
     * User.
     *
     * @return pager
     */
    @Override
    @Transactional
    public Pager getLocalePagerAll() {
        Pager pager = new Pager();
        long countAll = localeDAO.getCountAll();
        pager.setNumOfRecords(countAll);
        return pager;
    }

    /**
     *
     * @param pager
     * @return
     */
    @Override
    @Transactional
    public List<Locale> listLocale(Pager pager) {
        List<Locale> locales = new ArrayList<Locale>();
        locales = localeDAO.getPageOfDataAll(pager);
        if (locales.size() > 1) {
            return locales;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean createProductType(ProductType productType) {
        productTypeDAO.save(productType);
        return false;
    }

    /**
     * Get all of the User {@link Pager} info. Currently, only get the number of all the
     * User.
     *
     * @return pager
     */
    @Override
    @Transactional
    public Pager getProductTypePagerAll() {
        Pager pager = new Pager();
        long countAll = productTypeDAO.getCountAll();
        pager.setNumOfRecords(countAll);
        return pager;
    }

    /**
     * TODO: Why this read method also need to be transactional.
     *
     * @param pager
     * @return
     */
    @Override
    @Transactional
    public List<ProductType> listProductType(Pager pager) {
        List<ProductType> productTypes = new ArrayList<ProductType>();
        productTypes = productTypeDAO.getPageOfDataAll(pager);
        if (productTypes.size() > 1) {
            return productTypes;
        } else {
            return null;
        }
    }

    public void setThemeDAO(ThemeDAO themeDAO) {
        this.themeDAO = themeDAO;
    }

    public void setRegionDAO(RegionDAO regionDAO) {
        this.regionDAO = regionDAO;
    }

    public void setFunctionDAO(FunctionDAO functionDAO) {
        this.functionDAO = functionDAO;
    }

    public void setLocaleDAO(LocaleDAO localeDAO) {
        localeDAO = localeDAO;
    }

    public void setProductTypeDAO(ProductTypeDAO ProductTypeDAO) {
        this.productTypeDAO = ProductTypeDAO;
    }
}
